package com.example.zrq1010110087.graceplayer.main;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.zrq1010110087.graceplayer.R;
import com.example.zrq1010110087.graceplayer.data.MediaPlayerUser;
import com.example.zrq1010110087.graceplayer.data.Music;
import com.example.zrq1010110087.graceplayer.data.Sdcard_MusicList;

import java.util.ArrayList;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;

public class MainActivity extends Activity {
   //设置全局引用
    private ImageButton imgBtn_Previous;
    private ImageButton imgBtn_Play;
    private ImageButton imgBtn_Stop;
    private ImageButton imgBtn_Next;
    private ListView list;
    private TextView left_time;
    private TextView right_time;
    private SeekBar seekBar;
    private ArrayList<Music> musicArrayList;
    private MediaPlayerUser mediaPlayerUser;
    private MusicBroadcastReceiver musicReceiver;
    private StatusChangedReceiver statusChangedReceiver;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewByI();
        bindReceiver();
        registerListener();
        initMusicList();
        initListView();
        checkMusicFile();

    }
    //绑定对象
    private void findViewByI(){

         imgBtn_Previous= (ImageButton) findViewById(R.id.button_previous);
        imgBtn_Play= (ImageButton) findViewById(R.id.button_play);
        imgBtn_Stop= (ImageButton) findViewById(R.id.button_stop);
        imgBtn_Next= (ImageButton) findViewById(R.id.button_next);
        list=(ListView)findViewById(R.id.listview);
        left_time=(TextView)findViewById(R.id.tv_leftTime);
        right_time=(TextView)findViewById(R.id.tv_rightTime);
        seekBar=(SeekBar)findViewById(R.id.seekBar);

    }
    //注册广播接收器
    private  void bindReceiver(){

         //注册MusicBroadcastReceiver
        IntentFilter musicFilter=new IntentFilter("android.intent.action.Music_BroadcastReceiver");
        musicReceiver=new MusicBroadcastReceiver();
        registerReceiver(musicReceiver,musicFilter);

        //注册StatusChangedReceiver
        IntentFilter ststusfilter=new IntentFilter("android.intent.action.Music_StatusChangedReceiver");
        statusChangedReceiver=new StatusChangedReceiver();
        registerReceiver(statusChangedReceiver,ststusfilter);

    }
    //改变图标状态的接收器
    private class StatusChangedReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {

            int status=intent.getIntExtra("status",MediaPlayerUser.MUSIC_STATUS_ERROR);
            switch (status){

                case MediaPlayerUser.MUSIC_STATUS_PAUSED:imgBtn_Play.setBackgroundResource(R.drawable.button_play);break;
                case MediaPlayerUser.MUSIC_STATUS_STARTED:imgBtn_Play.setBackgroundResource(R.drawable.button_pause);break;
                case MediaPlayerUser.MUSIC_STATUS_STOP:imgBtn_Play.setBackgroundResource(R.drawable.button_play);break;


            }
        }
    }
    //发送广播
    private  void sendBroadcast_Choose(int status, int i){

       Intent intent=new Intent("android.intent.action.Music_BroadcastReceiver");

        switch (status){

            case MediaPlayerUser.MUSIC_COMMAND_PALY:intent.putExtra("status",MediaPlayerUser.MUSIC_COMMAND_PALY); break;
//            case MUSIC_COMMAND_PAPUSE:intent.putExtra("status",MUSIC_COMMAND_PAPUSE); break;
//            case MUSIC_COMMAND_RESUME:intent.putExtra("status",MUSIC_COMMAND_RESUME); break;
            case MediaPlayerUser.MUSIC_COMMAND_STOP:intent.putExtra("status",MediaPlayerUser.MUSIC_COMMAND_STOP); break;
            case  MediaPlayerUser.MUSIC_COMMAND_PREVIOUS:intent.putExtra("status",MediaPlayerUser.MUSIC_COMMAND_PREVIOUS);break;
            case  MediaPlayerUser.MUSIC_COMMAND_NEXT:intent.putExtra("status",MediaPlayerUser.MUSIC_COMMAND_NEXT);break;
            case MediaPlayerUser.MUSIC_COMMAND_ONCLICKITEM_NUMBER:intent.putExtra("status",MediaPlayerUser.MUSIC_COMMAND_ONCLICKITEM_NUMBER);
                                                        intent.putExtra("number",i);break;



        }

      sendBroadcast(intent);


    }

    //获得手机SDcard的音乐信息
     private void initMusicList(){

        musicArrayList= Sdcard_MusicList.getMusicArrayList(this);

    }


    //点击事件绑定
    private void registerListener(){

        imgBtn_Previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

           sendBroadcast_Choose(MediaPlayerUser.MUSIC_COMMAND_PREVIOUS,0);


            }
        });
        imgBtn_Play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            sendBroadcast_Choose(MediaPlayerUser.MUSIC_COMMAND_PALY,0);
            }
        });
        imgBtn_Stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                sendBroadcast_Choose(MediaPlayerUser.MUSIC_COMMAND_STOP,0);


            }
        });
        imgBtn_Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                sendBroadcast_Choose(MediaPlayerUser.MUSIC_COMMAND_NEXT,0);

            }
        });
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

               sendBroadcast_Choose(MediaPlayerUser.MUSIC_COMMAND_ONCLICKITEM_NUMBER,i);

           }
       });

    }
    //检查SD卡是否存在音乐,限制用户操作
    private void checkMusicFile(){

           if(musicArrayList.isEmpty()){

               imgBtn_Next.setEnabled(false);
               imgBtn_Play.setEnabled(false);
               imgBtn_Previous.setEnabled(false);
               imgBtn_Stop.setEnabled(false);
               makeText(MainActivity.this,this.getString(R.string.musiclist_null), LENGTH_SHORT).show();

           }
           else{


               imgBtn_Next.setEnabled(true);
               imgBtn_Play.setEnabled(true);
               imgBtn_Previous.setEnabled(true);
               imgBtn_Stop.setEnabled(true);

           }


    }

   //将获得的音乐信息根据需要以列表元素信息显示
    private  void initListView(){

         list.setAdapter(new MyAdapter());

    }
    //自定义适 配器
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return musicArrayList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v;
            if(view==null){

                v=View.inflate(MainActivity.this,R.layout.listview_items,null);
            }else{

                v=view;
            }
            TextView tv_number=(TextView)v.findViewById(R.id.tv_listview_item_number);
            TextView tv_title=(TextView)v.findViewById(R.id.tv_listview_item_title);
            TextView tv_artist=(TextView)v.findViewById(R.id.tv_listview_item_artist);
            tv_artist.setText(musicArrayList.get(i).getmusicArtist());
            tv_title.setText(musicArrayList.get(i).getmusicName());
            tv_number.setText(i+"");

            return v;
        }
    }
}
